package org.acooke.uykfd

import java.io.File

import org.apache.log4j.BasicConfigurator
import org.apache.log4j.ConsoleAppender
import org.apache.log4j.Level
import org.apache.log4j.SimpleLayout
import org.apache.log4j.varia.LevelRangeFilter

import org.acooke.uykfd.db.Database
import org.acooke.uykfd.cmd.Dispatcher
import org.acooke.uykfd.cmd.ExitException
import org.acooke.uykfd.cmd.Reader

object Main {

  val filter = new LevelRangeFilter

  def setLogging(level: Level) = filter.setLevelMin(level)

  def initLogging = {
    setLogging(Level.FATAL)
//    setLogging(Level.DEBUG)
    val stdout = new ConsoleAppender(new SimpleLayout)
    stdout.addFilter(filter)
    stdout.activateOptions
    BasicConfigurator.configure(stdout)
  }

  def main(args: Array[String]) {    
    initLogging
    val database = new Database
    try {
      try {
        if (args.length > 0) {
          Dispatcher(args.mkString("", " ", ""), database.cnxn)
        } else {
          Reader(database.cnxn)
        }
      } finally {
        database.close
      }
    } catch {
      case x: ExitException => println("bye")
    }
  }

}
